/*
 * @FileName: [AbstractService.java]
 * 
 * @Package com.bcinfo.alumni.service
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary
 * information of BCINFO Technology Limited Company
 * ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement
 * you entered into with RKY.
 * 
 * $Rev: 219 $
 * $LastChangedDate$
 * $LastChangedBy: huangb $
 * 
 * @category ALUMNI
 * 
 * @version 1.1
 * 
 * @author $Author: huangb $
 * 
 * Change History:[Formatter: author date description] <br/>
 * 1
 * 2
 * 3
 */

package com.bci.pwtz.common.service;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.util.PaginationList;

/**
 * AbstractService <br/>
 * <p>
 * Description:<br/>
 * <p>
 * AbstractService
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *      public class Test extends  AbstractService
 * </PRE>
 * <p>
 */
public abstract class AbstractService
{
    protected final Logger       _log         = Logger.getLogger(this.getClass());

    /**
     * @Fields request : 自动注入的HttpServletRequest 变量
     */
    @Autowired
    protected HttpServletRequest request;

    /**
     * 验证token是否有效 <br/>
     * <p>
     * Description: 验证用户传来的token是否有效 <br/>
     * <p>
     * Author: yun<br/>
     * <p>
     * Date: 2013-10-15-上午10:05:21<br/>
     * <p>
     * 
     * @param token
     * @return
     * 
     */
    protected boolean validateToken(String token)
    {
        return true;
        // return RedisClientUtil.getShardedJedis().exists(token);
    }

    
    /**
     * 获取扩展名<br/>
     * <p>
     * Description: 如果文件名中不包含.则直接返回原文件名 <br/>
     * <p>
     * Author: yun<br/>
     * <p>
     * Date: 2013-10-17-下午2:03:38<br/>
     * <p>
     * 
     * @param fileName
     * @return
     * 
     */
    protected String getExtName(String fileName)
    {
        if (StringUtils.isNotBlank(fileName) && fileName.indexOf(".") > 0)
        {
            return fileName.substring(fileName.lastIndexOf("."));
        }

        return fileName;
    }

    /**
     * 获取pageSize参数 <br/>
     * <p>
     * Description: 获取params中的pageSize参数，读取异常时返回默认值 AlumniInterfaceConstants.INTERFACE_DEFAULT_PAGESIZE <br/>
     * <p>
     * Author: yun<br/>
     * <p>
     * Date: 2013-10-21-上午10:51:51<br/>
     * <p>
     * 
     * @return
     * 
     */
    protected int getPageSize()
    {
        int pageSize = 0;
        if (pageSize == 0)
        {
            pageSize = PwtzConstants.DEFAULT_PAGESIZE;
        }
        return pageSize;
    }
    @SuppressWarnings("rawtypes")
    protected List<Object> objToMap(List listObj, Set<String> nameSet)
    {
        List<Object> list = new ArrayList<Object>();
        for (Object obj : listObj)
        {
            Class<?> classType = obj.getClass();

            Map<String, Object> map = new HashMap<String, Object>();
            for (String attrName : nameSet)
            {
                try
                {
                    String getMethodName = "get" + attrName.substring(0, 1).toUpperCase() + attrName.substring(1);
                    Method getMethod = classType.getMethod(getMethodName, new Class[]
                    {});
                    map.put(attrName, getMethod.invoke(obj, new Object[]
                    {}));
                }
                catch (Exception e)
                {
                    if (_log.isInfoEnabled())
                    {
                        _log.info(ExceptionUtils.getStackTrace(e));
                    }

                    continue;
                }
            }

            list.add(map);
        }

        return list;
    }

    @SuppressWarnings(
    { "rawtypes" })
    protected PaginationList<Object> jsonIgnore(PaginationList pagination, Set<String> nameSet)
    {
        PaginationList<Object> _pagination = new PaginationList<Object>();
        _pagination.setTotalPage(pagination.getTotalPage());
        _pagination.setCurrPage(pagination.getCurrPage());
        _pagination.setPageSize(pagination.getPageSize());
        _pagination.setTotalRecord(pagination.getTotalRecord());
        _pagination.setRecords(objToMap(pagination.getRecords(), nameSet));

        return _pagination;
    }

    
}